package com.chinanet.repository.faultanticipate;


import com.chinanet.entry.faultanticipate.Record;
import com.querydsl.jpa.JPQLQuery;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;

public interface RecordRepository extends PagingAndSortingRepository<Record, Long>,QuerydslPredicateExecutor<Record>{
    <T> Page<T> findAll(JPQLQuery jpqlQuery, Pageable pageable);

    Record findByModelId(@Param("modelId") String modelId);

    @RestResource(path = "branch", rel = "branch")
    @Query(value = "select * from record where branch_id=:branchId", nativeQuery = true)
    Page<Record> getAllByBranch(@Param("branchId") long branchId, Pageable pageable);

    @RestResource(path = "lately", rel = "lately")
    @Query(value = "select *,max(last_modified_at) from record where last_modified_at<=now() group by branch_id having branch_id=:branchId", nativeQuery = true)
    Record getBylately(@Param("branchId") long branchId);

}